package com.minelx.woods.task.core.sql;

import com.alibaba.fastjson.JSON;
import com.minelx.sqlstructures.common.Files;
import com.minelx.sqlstructures.core.database.JsonDatabase;
import com.minelx.sqlstructures.statements.clause.Sql;
import com.minelx.statementsplus.IChildSqlBuilder;
import com.minelx.woods.task.core.FieldCollector;
import com.minelx.woods.task.core.values.FieldPathValues;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

public class SqlCollections {

	private final List<String> childrenSql;

	private final FieldCollector fieldCollector;

	public SqlCollections() {
		childrenSql = new LinkedList<>();
		fieldCollector = new FieldCollector();
	}

	public List<String> collections() {
		return Stream.concat(
						fieldCollector.toChildSqlBuilders().stream().map(IChildSqlBuilder::asSql).map(Sql::toText),
						childrenSql.stream())
				.collect(toList());
	}

	public void addFieldPath(FieldPathValues added) {
		fieldCollector.add(added);
	}

	public void addSqlContent(String content) {
		childrenSql.add(content);
	}

	public static JsonDatabase remoteDatabase() {
		return new JsonDatabase(JSON.parseObject(Files.toString("/databases/2_newpay.json")));
	}
}
